﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Collections.Concurrent;

namespace PMKB_Web
{
    class Interface_ModelBinder<T,InterfaceOf_T> : DefaultModelBinder
        where T : InterfaceOf_T, new()
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            var obj = new T();
            modelType = obj.GetType();
            return obj;
        }
    }
    class Interface_ModelBinderProvider : Dictionary<Type, IModelBinder>, IModelBinderProvider
    {
        public void AddInterface<T, InterfaceOf_T>()
             where T : InterfaceOf_T, new()
        {
            this.Add( typeof(InterfaceOf_T), new Interface_ModelBinder<T, InterfaceOf_T>());
        } 
        public IModelBinder GetBinder(Type modelType)
        {
            if (this.ContainsKey(modelType))
                return this[modelType];
            else
                return null;
        }
    }

}
